package com.lanchetech.bean.strategy;


import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

public class OrderMergeStrategy extends AbstractMergeStrategy {

    private List<Integer> orderSkuCountList;
    private Sheet sheet;

    public OrderMergeStrategy(List<Integer> orderSkuCountList) {
        this.orderSkuCountList = orderSkuCountList;
    }

    // 按照分组将各种类别分别合并成一个单元格
    private void mergeGroupColumn(Integer index) {
        Integer rowCnt = 1;
        for (Integer count : orderSkuCountList) {
            if(count > 1){
                CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCnt, rowCnt + count - 1, index, index);
                sheet.addMergedRegionUnsafe(cellRangeAddress);
            }
            rowCnt += count;
        }
    }

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, int i) {
        this.sheet = sheet;
        if (cell.getRowIndex() == 1) {
            switch (cell.getColumnIndex()) {
                case 0:
                case 1:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                    this.mergeGroupColumn(cell.getColumnIndex());
                    break;
                default:
                    break;
            }
        }
    }
}
